package samfeng.erp.product.service;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import samfeng.erp.product.attribute.dal.dataobject.AttributeDO;
import samfeng.erp.product.attribute.dal.mysql.AttributeMapper;
import samfeng.erp.product.category.dal.dataobject.ProductCategoryDO;
import samfeng.erp.product.category.dal.mysql.ProductCategoryMapper;
import samfeng.erp.product.controller.admin.vo.ProductCreateReqVO;
import samfeng.erp.product.controller.admin.vo.ProductExportReqVO;
import samfeng.erp.product.controller.admin.vo.ProductPageReqVO;
import samfeng.erp.product.controller.admin.vo.ProductUpdateReqVO;
import samfeng.erp.product.convert.ProductConvert;
import samfeng.erp.product.dal.dataobject.ProductDO;
import samfeng.erp.product.dal.mysql.ProductMapper;
import samfeng.erp.product.specification.dal.dataobject.SpecificationDO;
import samfeng.erp.product.specification.dal.mysql.SpecificationMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static samfeng.erp.product.enmus.ErrorCodeConstants.PRODUCT_NOT_EXISTS;

/**
 * 商品 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ProductServiceImpl implements ProductService {

    @Resource
    private ProductMapper productMapper;

    @Resource
    private AttributeMapper attributeMapper;

    @Resource
    private SpecificationMapper specificationMapper;

    @Resource
    private ProductCategoryMapper productCategoryMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long create(ProductCreateReqVO createReqVO) {
        // 插入
        ProductDO productDO = ProductConvert.INSTANCE.convert(createReqVO);
        productMapper.insert(productDO);
        // 产品属性
        List<AttributeDO> attribute = createReqVO.getAttribute();
        if (attribute.size() > 0) {
            for (AttributeDO productAttribute : attribute) {
                productAttribute.setProductId(productDO.getId());
                attributeMapper.insert(productAttribute);
            }
        }
        // 产品参数
        List<SpecificationDO> specification = createReqVO.getSpecification();
        if (specification.size() > 0) {
            for (SpecificationDO productSpecification : specification) {
                productSpecification.setProductId(productDO.getId());
                specificationMapper.insert(productSpecification);
            }
        }
        // 返回
        return productDO.getId();
    }

    @Override
    public void update(ProductUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateExists(updateReqVO.getId());
        // 更新
        ProductDO updateObj = ProductConvert.INSTANCE.convert(updateReqVO);
        // 产品属性
        List<AttributeDO> attribute = updateReqVO.getAttribute();
        if (null != attribute && attribute.size() > 0) {
            for (AttributeDO productAttribute : attribute) {
                if (null != productAttribute.getId()) {
                    attributeMapper.update(productAttribute, new LambdaQueryWrapperX<AttributeDO>()
                            .eq(AttributeDO::getId, productAttribute.getId()));
                } else {
                    productAttribute.setProductId(updateReqVO.getId());
                    attributeMapper.insert(productAttribute);
                }
            }
        }
        // 产品参数
        List<SpecificationDO> specification = updateReqVO.getSpecification();
        if (null != specification && specification.size() > 0) {
            for (SpecificationDO productSpecification : specification) {
                if (null != productSpecification.getId()) {
                    specificationMapper.update(productSpecification, new LambdaQueryWrapperX<SpecificationDO>()
                            .eq(SpecificationDO::getId, productSpecification.getId()));
                } else {
                    productSpecification.setProductId(updateReqVO.getId());
                    specificationMapper.insert(productSpecification);
                }
            }
        }
        productMapper.updateById(updateObj);
    }

    @Override
    public void delete(Long id) {
        // 校验存在
        this.validateExists(id);
        // 删除
        productMapper.deleteById(id);
    }

    private void validateExists(Long id) {
        if (productMapper.selectById(id) == null) {
            throw exception(PRODUCT_NOT_EXISTS);
        }
    }

    @Override
    public ProductDO get(Long id) {
        return productMapper.selectById(id);
    }

    @Override
    public List<ProductDO> getList(Collection<Long> ids) {
        return productMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ProductDO> getPage(ProductPageReqVO pageReqVO) {
        PageResult<ProductDO> productDOPageResult = productMapper.selectPage(pageReqVO);
        List<ProductDO> productDOList = productDOPageResult.getList();
        for (ProductDO productDO : productDOList) {
            if (null != productDO.getLastCategoryId()) {
                ProductCategoryDO productCategoryDO = productCategoryMapper.selectOne("id", productDO.getLastCategoryId());
                List<Long> productCategoryIdList = productCategoryMapper.productCategoryIdList(productCategoryDO);
                productDO.setCategoryId(productCategoryIdList);
            }
            List<AttributeDO> attributeDOList = attributeMapper.selectList("product_id", productDO.getId());
            if (attributeDOList.size() > 0) {
                productDO.setAttribute(attributeDOList);
            }
            List<SpecificationDO> specificationDOList = specificationMapper.selectList("product_id", productDO.getId());
            if (specificationDOList.size() > 0) {
                productDO.setSpecification(specificationDOList);
            }
        }
        return productDOPageResult;
    }

    @Override
    public List<ProductDO> getList(ProductExportReqVO exportReqVO) {
        return productMapper.selectList(exportReqVO);
    }

    @Override
    public void updateProuctStatus(String status, String id) {
        this.validateExists(Long.valueOf(id));
        productMapper.updateStateById(status, id);
    }

}
